package com.yfp.machine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.machine.domain.MealReplacement;
import com.yfp.machine.service.MealReplacementService;
import com.yfp.machine.mapper.MealReplacementMapper;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【meal_replacement(代取餐状态表)】的数据库操作Service实现
 * @createDate 2025-01-16 14:16:57
 */
@Service
public class MealReplacementServiceImpl extends ServiceImpl<MealReplacementMapper, MealReplacement> implements MealReplacementService {


    @Override
    public List<MealReplacement> getByMachineId(String machineId) {
        LambdaQueryWrapper<MealReplacement> query = Wrappers.lambdaQuery(MealReplacement.class);
        query.eq(MealReplacement::getMachineId, machineId);

        return this.list(query);
    }

    @Override
    public Boolean getOperationalOnlineStatus(String machineId) {
        List<MealReplacement> mealReplacementDOS = this.getByMachineId(machineId);
        // 根据用户id分组，并且获取分组后按照时间排序的最后一条数据
        Map<Long, MealReplacement> collect = mealReplacementDOS.stream().collect(Collectors.toMap(MealReplacement::getUserId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(MealReplacement::getId))));
        // 状态存在1则返回true否则false
        return collect.values().stream().map(MealReplacement::getStatus).anyMatch(status -> status.equals(1));
    }
}




